﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.HinhThucKyLuatService;
namespace eTMS.Controllers{
public class HinhThucKyLuatControllerBase : CommonController{
	public HinhThucKyLuatControllerBase()
		: base()
	{	
		this.dao = new HinhThucKyLuatServiceClient();
	}
	~HinhThucKyLuatControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private HinhThucKyLuatEntity oHinhThucKyLuat;	
	public HinhThucKyLuatEntity HinhThucKyLuatObject{
		get{
			return oHinhThucKyLuat;
		}
		set{
			oHinhThucKyLuat=value;
		}
	}
	protected HinhThucKyLuatServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSHinhThucKyLuat(false);			
			return _Table;
		}
	}
	public void LoadAllDSHinhThucKyLuat(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oHinhThucKyLuat==null)return false;
		bool result=true;
		try{
			oHinhThucKyLuat = dao.SaveRet(oHinhThucKyLuat);
		}
		catch{
			result=false;
		}
		if(result){
			_ListHinhThucKyLuat.Add(oHinhThucKyLuat);
		}
		return result;
	}
	public bool Update(){
		if(oHinhThucKyLuat==null)return false;
		return dao.Update(oHinhThucKyLuat);
	}
	public bool Delete(){
		if(oHinhThucKyLuat==null)return false;
		bool retvalue = dao.Delete(oHinhThucKyLuat);
		if(retvalue){			
			_ListHinhThucKyLuat.Remove(oHinhThucKyLuat);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 HinhThucKyLuat_ID){
		bool retvalue = dao.DeleteByID(HinhThucKyLuat_ID);		
		if(retvalue){
			HinhThucKyLuatEntity oDeleted=FindHinhThucKyLuat(HinhThucKyLuat_ID);
			if(oDeleted!=null){
				_ListHinhThucKyLuat.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oHinhThucKyLuat = new HinhThucKyLuatEntity();
	}
	//Check if exists MaHinhThucKyLuat
	public bool CheckExistsMaHinhThucKyLuat(object value){	
		return dao.SelectOneByMaHinhThucKyLuat(value.ToString()).MaHinhThucKyLuat!=null;
    }    
    //Copy data from data row in grid to object
    public HinhThucKyLuatEntity MapDataRowToObject(DataRow row){
		HinhThucKyLuatEntity o = new HinhThucKyLuatEntity();
		 o.MaHinhThucKyLuat=Convert.ToString(row["MaHinhThucKyLuat"]);
		 o.TenHinhThucKyLuat=Convert.ToString(row["TenHinhThucKyLuat"]);
		 o.MoTaHinhThucKyLuat=Convert.ToString(row["MoTaHinhThucKyLuat"]);
		 if(!row.IsNull("HinhThucKyLuat_ID"))
		    o.HinhThucKyLuat_ID=(Int32)(row["HinhThucKyLuat_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(HinhThucKyLuatEntity o,DataRow row){
		row["MaHinhThucKyLuat"]=o.MaHinhThucKyLuat;
		row["TenHinhThucKyLuat"]=o.TenHinhThucKyLuat;
		row["MoTaHinhThucKyLuat"]=o.MoTaHinhThucKyLuat;
		row["HinhThucKyLuat_ID"]=o.HinhThucKyLuat_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.HinhThucKyLuatObject,row);
    }
	public void LoadObject(Int32 HinhThucKyLuat_ID){
		HinhThucKyLuatEntity oLoad=FindHinhThucKyLuat(HinhThucKyLuat_ID);
		if(oLoad==null){
			oHinhThucKyLuat=this.LoadHinhThucKyLuat(HinhThucKyLuat_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oHinhThucKyLuat.IsNullHinhThucKyLuat_ID;
			if(!bNotIsNull){
				this.ListHinhThucKyLuat.Add(oHinhThucKyLuat);
			}
		}
		else{
			oHinhThucKyLuat=oLoad;
		}
	}
	#region CachedList HinhThucKyLuat
	private static List<HinhThucKyLuatEntity> _ListHinhThucKyLuat=new List<HinhThucKyLuatEntity>();
    public List<HinhThucKyLuatEntity>ListHinhThucKyLuat{
		get{
			return _ListHinhThucKyLuat;
		}
	}
	public static List<HinhThucKyLuatEntity>ListHinhThucKyLuatStatic{
		get{
			return _ListHinhThucKyLuat;
		}
	}
	public HinhThucKyLuatEntity FindHinhThucKyLuat(Int32 HinhThucKyLuat_ID){
		HinhThucKyLuatEntity oResult=this.ListHinhThucKyLuat.Find(delegate(HinhThucKyLuatEntity oFind){
			bool result=true; 
			result&=oFind.HinhThucKyLuat_ID.Equals(HinhThucKyLuat_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllHinhThucKyLuat(bool clearCache){
		if(_ListHinhThucKyLuat.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		HinhThucKyLuatServiceClient dao = new HinhThucKyLuatServiceClient();
		_ListHinhThucKyLuat=new List<HinhThucKyLuatEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public HinhThucKyLuatEntity LoadHinhThucKyLuat(Int32 HinhThucKyLuat_ID){			
		HinhThucKyLuatEntity o=dao.SelectOne(HinhThucKyLuat_ID);		
		return o;
	}
	#endregion
}
}
